<?php
session_start();

// 当前用户
$currentUser = $_SESSION['username'] ?? null;
$targetUserId = $_GET['id'] ?? null;

// 验证登录状态和目标用户
if (!$currentUser) {
    die(json_encode(['status' => 'error', 'message' => '用户未登录！']));
}

if (!$targetUserId) {
    die(json_encode(['status' => 'error', 'message' => '未指定好友！']));
}

// 用户聊天列表文件路径
$userChatsFile = __DIR__ . "/user/{$currentUser}/chats.json";

// 如果文件存在，则读取并删除指定好友的记录
if (file_exists($userChatsFile)) {
    $chats = json_decode(file_get_contents($userChatsFile), true);
    
    // 过滤掉被删除的好友
    $chats = array_filter($chats, function($chat) use ($targetUserId) {
        return $chat['id'] !== $targetUserId;
    });
    
    // 重新索引数组
    $chats = array_values($chats);
    
    // 保存更新后的聊天列表
    file_put_contents($userChatsFile, json_encode($chats, JSON_UNESCAPED_UNICODE));
}

// 显示过渡页面
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>记录已删除 - JIIL微聊</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', sans-serif;
        }
        
        body {
            background: linear-gradient(135deg, #f5f7fa 0%, #e4e7eb 100%);
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
        }
        
        .container {
            max-width: 500px;
            width: 100%;
        }
        
        .success-card {
            background: white;
            border-radius: 20px;
            box-shadow: 0 15px 50px rgba(0, 0, 0, 0.1);
            overflow: hidden;
            text-align: center;
            padding: 50px 30px;
            position: relative;
        }
        
        .success-icon {
            position: relative;
            width: 100px;
            height: 100px;
            margin: 0 auto 30px;
        }
        
        .check-circle {
            position: absolute;
            top: 0;
            left: 0;
            width: 100px;
            height: 100px;
            background: #4CAF50;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        
        .check-mark {
            position: relative;
            width: 40px;
            height: 40px;
        }
        
        .check-mark::before {
            content: '';
            position: absolute;
            top: 50%;
            left: 0;
            width: 20px;
            height: 4px;
            background: white;
            transform: translateY(-50%) rotate(45deg);
            border-radius: 2px;
        }
        
        .check-mark::after {
            content: '';
            position: absolute;
            top: 50%;
            left: 0;
            width: 40px;
            height: 4px;
            background: white;
            transform: translateY(-50%) rotate(-45deg);
            border-radius: 2px;
        }
        
        .success-title {
            font-size: 26px;
            font-weight: 700;
            color: #2d3748;
            margin-bottom: 15px;
            letter-spacing: 0.5px;
        }
        
        .success-message {
            font-size: 18px;
            color: #4CAF50;
            font-weight: 500;
            margin-bottom: 25px;
        }
        
        .redirect-message {
            font-size: 16px;
            color: #718096;
            line-height: 1.7;
            max-width: 400px;
            margin: 0 auto 30px;
        }
        
        .countdown {
            font-size: 24px;
            font-weight: 700;
            color: #3498db;
            margin-top: 20px;
        }
        
        .btn-container {
            margin-top: 30px;
        }
        
        .btn {
            padding: 12px 28px;
            border-radius: 8px;
            font-weight: 600;
            font-size: 15px;
            cursor: pointer;
            transition: all 0.2s ease;
            text-decoration: none;
            display: inline-block;
            background: #3498db;
            color: white;
            border: none;
        }
        
        .btn:hover {
            background: #2980b9;
            transform: translateY(-2px);
        }
        
        @media (max-width: 576px) {
            .success-card {
                padding: 40px 20px;
            }
            
            .success-title {
                font-size: 22px;
            }
            
            .success-message {
                font-size: 16px;
            }
            
            .redirect-message {
                font-size: 14px;
            }
            
            .btn {
                padding: 10px 20px;
                font-size: 14px;
                width: 100%;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="success-card">
            <div class="success-icon">
                <div class="check-circle">
                    <div class="check-mark"></div>
                </div>
            </div>
            
            <h1 class="success-title">好友记录已删除</h1>
            <div class="success-message">操作成功完成</div>
            
            <p class="redirect-message">
                你已成功删除与 <strong><?= htmlspecialchars($targetUserId) ?></strong> 的好友记录。
                页面将在 <span id="countdown">3</span> 秒后自动返回主页。
            </p>
            
            <div class="countdown" id="countdown-display">3</div>
            
            <div class="btn-container">
                <a href="index.php" class="btn">
                    <i class="fas fa-home"></i> 立即返回主页
                </a>
            </div>
        </div>
    </div>
    
    <script>
        // 倒计时功能
        let seconds = 3;
        const countdownElement = document.getElementById('countdown-display');
        
        function updateCountdown() {
            countdownElement.textContent = seconds;
            seconds--;
            
            if (seconds >= 0) {
                setTimeout(updateCountdown, 1000);
            } else {
                window.location.href = 'index.php';
            }
        }
        
        // 开始倒计时
        updateCountdown();
    </script>
</body>
</html>
<?php
exit();
?>